package com.imu.purse.modules.sms.dao;

import java.util.List;
import org.apache.ibatis.annotations.Select;
import com.imu.purse.modules.sms.entity.SmsLogEntity;
import com.imu.core.base.BaseMapper;

/**
 * 短信记录
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2018-03-21 14:09:34
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
public interface SmsLogMapper extends BaseMapper<SmsLogEntity>
{
	/**
     * 分页查询
     * @return
     */
	@Select({
		"<script>",
		    "SELECT id,",
					"phone, ",
					"code, ",
					"state, ",
					"time_create, ",
					"content, ",
					"desc",
		    "FROM imu_sms_log ",
		    "WHERE status != -1",
			"<when test = 'phone != null'>",
		    	"AND `phone` = #{phone}",
		    "</when>",
			"<when test = 'code != null'>",
		    	"AND `code` = #{code}",
		    "</when>",
			"<when test = 'state != null'>",
		    	"AND `state` = #{state}",
		    "</when>",
			"<when test = 'timeCreate != null'>",
		    	"AND `time_create` = #{timeCreate}",
		    "</when>",
			"<when test = 'content != null'>",
		    	"AND `content` = #{content}",
		    "</when>",
			"<when test = 'desc != null'>",
		    	"AND `desc` = #{desc}",
		    "</when>",
		    "<when test = 'sts != null'>",
		    	"AND time_create &gt;= #{sts}",
		    "</when>",
		    "<when test = 'ets != null'>",
				"AND time_create &lt;= #{ets}",
			"</when>",
		    "ORDER BY cs DESC",
    	"</script>"
	})
    public List<SmsLogEntity> findByPage(final SmsLogEntity entity);

	/**
	 * 根据手机号码和类型获取在指定时间内短信调用的数量
	 * @param entity
	 * @return
	 */
	@Select(
		{
			"<script>",
				"select count(1) from imu_sms_log where phone = #{phone} and state=1 ",
				"<if test='unit == 1'> ",
					" and time_create &gt;= DATE_ADD(#{timeCreate},INTERVAL #{interval} MINUTE)",
				"</if>",
				"<if test='unit == 2'>",
					" and time_create &gt;= DATE_ADD(#{timeCreate},INTERVAL #{interval} hour)",
				"</if>",
				"<if test='unit == 3'>",
					" and time_create &gt;= DATE_ADD(#{timeCreate},INTERVAL #{interval} day)",
				"</if>",
				" and time_create &lt;= #{timeCreate}",
			"</script>"
		}
	)
	public int queryCountByPhAndTime(final  SmsLogEntity entity);
}
